#!/bin/bash
#
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

# shellcheck source=/dev/null
source "$(cd "$(dirname "$0")" && pwd)/functions.sh"

fabric_ca_dir="$(cd "$(dirname "$0")/.." && pwd)"
source_dirs=()
while IFS=$'\n' read -r source_dir; do
    source_dirs+=("$source_dir")
done < <(go list -f '{{.Dir}}' ./... | sed s,"${fabric_ca_dir}".,,g | cut -f 1 -d / | sort -u)

echo "Checking imports ..."
OUTPUT="$("$fabric_ca_dir/build/tools/goimports" -l "${source_dirs[@]}")"
OUTPUT="$(filterExcludedAndGeneratedFiles "$OUTPUT")"
if [[ "$OUTPUT" != "" ]]; then
    echo "The following files have import problems:"
    echo "$OUTPUT"
    echo "You may run 'goimports -w <file>' to fix each file."
    exit 1
fi
echo "All files are properly formatted"
